package test.domain.environment.node;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class Server extends Node {
	private String domain;
	private String serviceName;
	
	public Server() {}
	
	public Server(String domain, int port, String serviceName) {
		this.domain = domain;
		this.serviceName = serviceName;
		
		this.setPort(port);
	}
	
	public static List<Server> initializeServers(Properties prop, String key) {
		List<Server> servers = new ArrayList<Server>();
		
		String serverProp = prop.getProperty(key, null);
		if (serverProp == null) throw new IllegalArgumentException("Server information is invalid.");
		
		if (serverProp.contains(",")) {
			// use multiple connection managers
			String[] serverInfos = serverProp.split(",");
			for (String serverInfo : serverInfos) {
				servers.add(initializeServer(serverInfo));
			}
		}
		else {
			// use openfire server itself
			servers.add(initializeServer(serverProp));
		}
				
		return servers;
	}
	
	private static Server initializeServer(String serverInfo) {
		String[] serverInfos = serverInfo.split(":");
		
		Server server = new Server();
		server.setDomain(serverInfos[0]);
		server.setPort(Integer.parseInt(serverInfos[1]));
		server.setServiceName(serverInfos[2]);
		
		return server;
	}
	
	public String getDomain() {
		return domain;
	}
	public void setDomain(String domain) {
		this.domain = domain;
	}
	public String getServiceName() {
		return serviceName;
	}
	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}	
}
